﻿@namespace AntSK.Pages.Setting.AIModel
@using AntSK.Domain.Repositories
@using AntSK.Models
@using AntSK.Domain.Domain.Model.Enum
@using AntSK.LLamaFactory.Model
@using AntSK.LLamaFactory
@using BlazorComponents.Terminal
@page "/modelmanager/model/add"
@page "/modelmanager/model/add/{ModelId}"
@page "/modelmanager/model/addbypath/{ModelPath}"
@using AntSK.Services.Auth
@inherits AuthComponentBase
@using Microsoft.AspNetCore.Authorization
@attribute [Authorize(Roles = "AntSKAdmin")]

<PageContainer Title="新增模型">
    <ChildContent>
        <Card>
            <Form Model="@_aiModel"
                  Style="margin-top: 8px;"
                  OnFinish="HandleSubmit">
                <FormItem Label="模型描述" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                    <Input Placeholder="请输入模型描述" @bind-Value="@context.ModelDescription" />
                </FormItem>

                <FormItem Label="AI类型" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                    <EnumRadioGroup @bind-Value="context.AIType" ButtonStyle="RadioButtonStyle.Solid" OnChange="AITypeChange" TEnum="AIType"> </EnumRadioGroup>
                </FormItem>
                <FormItem Label="模型类型" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                    <RadioGroup @bind-Value="context.AIModelType">
                        @if (context.AIType == AIType.StableDiffusion)
                        {
                            <Radio RadioButton Value="@(AIModelType.Image)">图片模型</Radio>
                        }
                        else
                        {
                            @if (context.AIType != AIType.BgeEmbedding && context.AIType != AIType.BgeRerank)
                            {
                                <Radio RadioButton Value="@(AIModelType.Chat)">会话模型</Radio>
                            }
                            @if (context.AIType != AIType.LLamaFactory && context.AIType != AIType.Mock && context.AIType != AIType.SparkDesk && context.AIType != AIType.BgeRerank)
                            {
                                <Radio RadioButton Value="@(AIModelType.Embedding)">向量模型</Radio>
                            }
                            @if (context.AIType == AIType.BgeRerank)
                            {
                                <Radio RadioButton Value="@(AIModelType.Rerank)">Rerank重排模型</Radio>
                            }
                        }
                    </RadioGroup>
                </FormItem>
                @if (context.AIModelType == AIModelType.Embedding)
                {
                    <FormItem Label="注意事项" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <span style="color:red"><b>请不要使用不同维度的向量模型，否则会导致无法向量存储</b></span>
                    </FormItem>
                }

                @if (context.AIType == AIType.AzureOpenAI)
                {
                    <FormItem Label="请求地址" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Input Placeholder="请输入请求地址" @bind-Value="@context.EndPoint" />
                    </FormItem>
                    <FormItem Label="部署名(模型名)" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Input Placeholder="请输入部署名" @bind-Value="@context.ModelName" />
                    </FormItem>
                    <FormItem Label="模型秘钥" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <InputPassword @bind-Value="@context.ModelKey" Placeholder="请输入模型秘钥"  />
                    </FormItem>
                }
                @if (context.AIType == AIType.OpenAI)
                {
                     <FormItem Label="代理API" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <a href="https://api.antsk.cn/" target="_blank">去创建</a>
                    </FormItem>
                    <FormItem Label="请求地址" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Input Placeholder="请输入请求地址 示例格式 http://ip:port/" @bind-Value="@context.EndPoint" />
                    </FormItem>
                    <FormItem Label="模型名称" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Input Placeholder="请输入模型名称" @bind-Value="@context.ModelName" />
                    </FormItem>
                    <FormItem Label="模型秘钥" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <InputPassword @bind-Value="@context.ModelKey" Placeholder="请输入模型秘钥" />
                    </FormItem>
                }
                @if (context.AIType == AIType.SparkDesk)
                {
                    <FormItem Label="APPID" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Input Placeholder="请输入APPID" @bind-Value="@context.EndPoint" />
                    </FormItem>
                    <FormItem Label="APISecret" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <InputPassword @bind-Value="@context.ModelKey" Placeholder="APISecret"  />
                    </FormItem>
                    <FormItem Label="APIKey" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Input Placeholder="请输入请输入APIKey" @bind-Value="@context.ModelName" />
                    </FormItem>
                   
                }
                @if (context.AIType == AIType.DashScope)
                {
                    <FormItem Label="API KEY" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Input Placeholder="请输入API KEY" @bind-Value="@context.ModelKey" />
                    </FormItem>
                    <FormItem Label="模型名称" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Input Placeholder="请输入模型名称" @bind-Value="@context.ModelName" />
                    </FormItem>
                }
                @if (context.AIType == AIType.LLamaSharp || context.AIType == AIType.StableDiffusion)
                {
                    <FormItem Label="模型路径" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <InputGroup>
                            <AutoComplete Options="_modelFiles" Placeholder="请输入模型路径" @bind-Value="@context.ModelName" />
                            <Button OnClick="()=>_downloadModalVisible=true">从Haggingface下载</Button>
                        </InputGroup>
                    </FormItem>
                }
                @if (context.AIType == AIType.LLamaFactory)
                {
                    <FormItem Label="模型名称" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <InputGroup>
                            <Select TItem="LLamaModel"
                                    TItemValue="string"
                                    DataSource="@modelList"
                                    @bind-Value="@context.ModelName"
                                    LabelName="@nameof(LLamaModel.Name)"
                                    ValueName="@nameof(LLamaModel.ModelScope)"
                                    Placeholder="选择模型"
                                    DefaultActiveFirstOption="false"
                                    EnableSearch
                                    OnSearch="OnSearch">
                            </Select>
                        </InputGroup>
                    </FormItem>
                    <FormItem Label="请求地址" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Input Placeholder="http://localhost:8080/" @bind-Value="@context.EndPoint" />
                    </FormItem>
                    <FormItem Label="环境安装" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Button Type="primary" OnClick="PipInstall">环境安装</Button>
                    </FormItem>
                    <FormItem Label="llama factory服务" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <InputGroup>
                            @if (!llamaFactoryIsStart)
                            {
                                <Button Type="primary" OnClick="StartLFService" Disabled="@(llamaFactoryIsStart)">启动</Button>
                            }
                            else
                            {
                                <Button OnClick="StopLFService" Disabled="@(!llamaFactoryIsStart)">停止</Button>
                            }
                        </InputGroup>
                    </FormItem>
                }

                @if (context.AIType == AIType.BgeEmbedding)
                {
                    <FormItem Label="模型名称" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Select DataSource="@bgeEmbeddingList"
                                @bind-Value="@context.ModelName"
                                ValueProperty="c=>c"
                                LabelProperty="c=>c">
                        </Select>
                    </FormItem>

                    <FormItem Label="Python路径" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Popover ContentTemplate="@_content" Title="提示" Style="width:100%">
                            <Input Placeholder="D:\Programs\Python\Python311\python311.dll" @bind-Value="@context.EndPoint" />
                        </Popover>
                    </FormItem>

                    <FormItem Label="环境安装" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Button Type="primary" OnClick="PipInstall">环境安装</Button>
                    </FormItem>
                    <FormItem Label="下载并初始化" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Spin Tip="请等待..." Spinning="@(BgeEmbeddingIsStart)">
                            <Button Type="primary" Disabled="@(BgeEmbeddingIsStart)" OnClick="BgeEmbedding">@BgeEmbeddingBtnText</Button>
                        </Spin>
                    </FormItem>
                }
                @if (context.AIType == AIType.BgeRerank)
                {
                    <FormItem Label="模型名称" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Select DataSource="@bgeRerankList"
                                @bind-Value="@context.ModelName"
                                ValueProperty="c=>c"
                                LabelProperty="c=>c">
                        </Select>
                    </FormItem>
                    <FormItem Label="Python路径" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Popover ContentTemplate="@_content" Title="提示" Style="width:100%">
                            <Input Placeholder="D:\Programs\Python\Python311\python311.dll" @bind-Value="@context.EndPoint" />
                        </Popover>
                    </FormItem>
                    <FormItem Label="环境安装" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Button Type="primary" OnClick="PipInstall">环境安装</Button>
                    </FormItem>
                    <FormItem Label="下载并初始化" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Spin Tip="请等待..." Spinning="@(BgeRerankIsStart)">
                            <Button Type="primary" Disabled="@(BgeRerankIsStart)" OnClick="BgeRerank">@BgeRerankBtnText</Button>
                        </Spin>
                    </FormItem>
                }
                @if (context.AIType == AIType.Mock)
                {
                }
                <FormItem Label=" " Style="margin-top:32px" WrapperCol="LayoutModel._submitFormLayout.WrapperCol">
                    <Button Type="primary" OnClick="HandleSubmit">
                        保存
                    </Button>
                    <Button OnClick="Back">
                        返回
                    </Button>
                </FormItem>
            </Form>
        </Card>
    </ChildContent>
</PageContainer>

<Modal @ref="_modal" Visible="_downloadModalVisible" Footer="null" Closable Title="模型下载" OnCancel="OnCancelDownload" DestroyOnClose>
    <Flex Gap="10" Vertical>
        <InputGroup>
            <Input Disabled="_downloadStarted" Placeholder="请输入下载地址" @bind-Value="_downloadUrl" Style="width:80%"></Input>
            @if (!_downloadStarted)
            {
                <Button OnClick="StartDownload">开始</Button>
            }
            else
            {
                <Button OnClick="StopDownload">停止</Button>
            }
        </InputGroup>
        <AntDesign.Progress Percent="_downloadProgress"></AntDesign.Progress>

    </Flex>
</Modal>

<Modal Visible="_logModalVisible" Footer="null" Closable Title="日志输出" OnCancel="OnCancelLog" DestroyOnClose Width="1000">
    <BlazorTerminal @ref="blazorTerminal" Style="height:600px;"></BlazorTerminal>
</Modal>


@code
{
    RenderFragment _content =
    @<div>
        <p>在windows上使用 python311.dll文件，在linux上使用 libpython3.10.so</p>
    </div>;
}